home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio / Patchmix / Source / Allpass.m < prev    next >
Text File  |  1992-08-18  |  6KB  |  243 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Allpass.h"
  5. #import "Instrum.h"
  6. #import <appkit/graphics.h>
  7. #import "Statement.h"
  8.  
  9. @implementation Allpass
  10.  
  11. + initialize
  12. {
  13.        image = [NXImage findImageNamed:"Allpass"];
  14.     rvtOffset.x = 3;
  15.     rvtOffset.y = 30;
  16.     inOffset.x = 30;
  17.     inOffset.y = 51;
  18.     freqOffset.x = 52;
  19.     freqOffset.y = 30;
  20.     outOffset.x = 29;
  21.     outOffset.y = 3;
  22.     
  23.     return self;
  24. }
  25.  
  26. - init
  27. {
  28.     written = NO;
  29.     size.width = 80.;
  30.     size.height = 80.;
  31.     center.x = size.width/2.;
  32.     center.y = size.height/2.;
  33.     strcpy(type,"Allpass");
  34.     strcpy(name,"allpass");    
  35.     
  36.     rvt = [[Param alloc] init:self :&rvtOffset];
  37.     in = [[Param alloc] init:self :&inOffset];
  38.     freq = [[Param alloc] init:self :&freqOffset];
  39.     out = [[Param alloc] init:self :&outOffset];
  40.  
  41.     [rvt setValue:".5"];        // set non-"1" default values
  42.     [freq setValue:"440"];    
  43.     [rvt setTitle:"RvbTi:"];
  44.     [freq setTitle:"LpTi"];
  45.     [in setTitle:"Input:"];
  46.     [out setTitle:"Out:"];
  47.         
  48.     paramList = [[List alloc] initCount:4];
  49.     [paramList addObject:rvt];
  50.     [paramList addObject:in];
  51.     [paramList addObject:freq];
  52.     [paramList addObject:out];
  53.     
  54.     [Inst putUgenInList:self];
  55.     
  56.     return self;
  57. }
  58.  
  59. - remove
  60. {
  61.     id cp,param;
  62.     int i;
  63.                             // make sure not connected first 
  64.     for(i = 0; i < ([paramList count]); i++) {    
  65.         param = [paramList objectAt:i];
  66.         if(cp = [param getConnectedParam]) 
  67.             return 0;
  68.     }
  69.                             // remove ugen from list
  70.     [Inst removeUgenFromList:self];
  71.     return self;
  72. }
  73.  
  74. - (NXImage *)getImage
  75. {
  76.     return image;
  77. }
  78.  
  79. - move:(NXPoint *)newloc
  80. {
  81.     location = *newloc;
  82.     [rvt move:&location];
  83.     [in move:&location];
  84.     [freq move:&location];
  85.     [out move:&location];
  86.     return self;
  87. }
  88.  
  89. - findParamAtPoint:(NXPoint *)point
  90. {
  91.     NXRect *rect;
  92.     int i;
  93.     id param;
  94.     
  95.     for(i = 0; i < [paramList count]; i++) {
  96.         param = [paramList objectAt:i];
  97.         rect = [param getRect];
  98.         if(NXMouseInRect(point,rect,NO))
  99.             return param;
  100.     }
  101.     return nil;
  102. }
  103.  
  104. - writeUgen
  105. {
  106.     BOOL ai = NO;    // amplitude input ugen
  107.     char ampVar[10];
  108.     char par[20];
  109.     /*     for params:
  110.             see if there is input ugen
  111.                 if so, grab the output (ug?) and mufreqiply it by this
  112.                     param value
  113.                 if not just use the param value
  114.          after all params done, write relevant code into lists
  115.             (declarations, assignments and loop statements)
  116.             i.e. ug? = oscil(,,,,);
  117.         go to output param connected ugen and call its "writeUgen"
  118.     */
  119.     int i;
  120.     int parent[4];
  121.     id param;
  122.     id ug;
  123.     id nupar;
  124.     char str[50];
  125.     
  126. //    printf("writing ugen %d\n",index);
  127.     sprintf(str,"\tfloat ug%d;\n",index);
  128.     [Inst putVarInList:str];
  129.     sprintf(str,"\tfloat amp%d;\n",index);
  130.     [Inst putVarInList:str];
  131.     sprintf(str,"\tfloat rvt%d;        // reverb time\n",index);
  132.     [Inst putVarInList:str];
  133.     sprintf(str,"\tfloat in%d;\n",index);
  134.     [Inst putVarInList:str];
  135.     sprintf(str,"\tfloat freq%d;\n",index);
  136.     [Inst putVarInList:str];
  137.     sprintf(str,"\tfloat out%d;\n",index);
  138.     [Inst putVarInList:str];
  139.     sprintf(str,"\tfloat loopt%d;         // loop time\n",index);
  140.     [Inst putVarInList:str];
  141.     sprintf(str,"\tfloat  *q%d;\n",index);
  142.     [Inst putVarInList:str];
  143.     sprintf(str,"\tfloat combarr%d[10000];\n",index);
  144.     [Inst putVarInList:str];
  145.  
  146.     for(i = 0; i < ([paramList count]-1); i++) {    
  147.         param = [paramList objectAt:i];
  148.         if(nupar = [param getConnectedParam]) {
  149.             ug = [nupar getUgen];
  150.             if([ug getWritten] == NO) { 
  151.                 parent[i] = [[ug writeUgen] getIndex];
  152.             }
  153.             else  {
  154.                 parent[i] = 0;
  155.             }
  156.         }
  157.         else 
  158.             parent[i] = 0;
  159.         
  160.     }
  161.  
  162.                                 // reverb time
  163.     strcpy(par,[[paramList objectAt:0] getValue]);
  164.     if(par[0] == 'p' && par[1] == '[')
  165.         sprintf(str,"\trvt%d = %s;\n", index, par);
  166.     else
  167.         sprintf(str,"\trvt%d = %.2f;\n", index, atof(par));
  168.     [Inst putAssignInList:str];
  169.     
  170.                                 // input sig
  171.     strcpy(par,[[paramList objectAt:1] getValue]);
  172.     if(par[0] == 'p' && par[1] == '[')
  173.         sprintf(str,"\tamp%d = %s;\n", index, par);
  174.     else
  175.         sprintf(str,"\tamp%d = %.2f;\n", index, atof(par));
  176.     [Inst putAssignInList:str];
  177.     
  178.                                 // pitch
  179.     strcpy(par,[[paramList objectAt:2] getValue]);
  180.     if(par[0] == 'p' && par[1] == '[')
  181.         sprintf(str,"\tloopt%d = 1/%s;\n", index, par);
  182.     else
  183.         sprintf(str,"\tloopt%d = 1/%.2f;\n", index, atof(par));
  184.     [Inst putAssignInList:str];
  185.  
  186.     if(parent[1]) {     //  input ugen    
  187.         ai = YES;
  188.         sprintf(str,"\tin%d = %.2f;\n",index, 
  189.             atof([[paramList objectAt:1] getValue]));
  190.         [Inst putAssignInList:str];
  191.         sprintf(str,"\tfloat inai%d;\n",index);
  192.         [Inst putVarInList:str];
  193.         sprintf(str,"\t\tinai%d = amp%d * ug%d;\n",index,  
  194.             index,parent[1]);
  195.         [Inst putLoopInList:str];
  196.     }
  197.     else {                // input preset or default
  198.          strcpy(par,[[paramList objectAt:0] getValue]);
  199.         if(par[0] == 'p' && par[1] == '[')
  200.             sprintf(str,"\tamp%d = %s;\n", index, par);
  201.         else
  202.             sprintf(str,"\tamp%d = %.2f;\n",index,atof(par));
  203.         [Inst putAssignInList:str];
  204.     }
  205.     if(ai)
  206.         sprintf(ampVar,"inai%d",index);
  207.     else
  208.         sprintf(ampVar,"amp%d",index);
  209.         
  210.     sprintf(str,"\tnsamps = nsamps + (rvt%d * SR);   // add reverbtime to out duration\n",  index, index, index);
  211.     [Inst putAssignInList:str];
  212.          
  213.                         //     allocate memory for q array
  214.     sprintf(str,"\tif((q%d = (float *)malloc((int)((loopt%d*SR+20) *  4))) == 0) {\n", index, index);
  215.     [Inst putAssignInList:str];
  216.     sprintf(str,"\t\tprintf(\"can't malloc memory for delayline\");\n\t\texit(-1);\n\t}\n");
  217.     [Inst putAssignInList:str];
  218.     
  219.                         //     set up comb filter
  220.     sprintf(str, "\tcombset(loopt%d,rvt%d,0,q%d);\n",index,index, index);
  221.     [Inst putAssignInList:str];
  222.     
  223.                         //     output amplitude multiplier:
  224.     strcpy(par,[[paramList objectAt:3] getValue]);
  225.     if(par[0] == 'p' && par[1] == '[')
  226.         sprintf(str,"\tout%d = %s;\n",index,par);
  227.     else
  228.         sprintf(str,"\tout%d = %.2f;\n", index, atof([[paramList objectAt:3] getValue]));
  229.     [Inst putAssignInList:str];
  230.  
  231.     sprintf(str,"\t\tug%d = %s(%s,q%d) * out%d;\n", index, name, ampVar, index, index);
  232.     [Inst putLoopInList:str];
  233.     
  234.     sprintf(str,"\tfree(q%d);\n", index);
  235.     [Inst putEndInList:str];
  236.  
  237.     written = YES;
  238.     return self;    
  239. }
  240.  
  241.  
  242. @end
  243.